import java.util.*;

/**
 * @author LKQ
 * @date 2022/3/26 16:28
 * @description
 */
public class Solution2 {
    public static void main(String[] args) {
        Solution2 solution = new Solution2();
        int[][] segments = {{4,16,12},{9,10,15},{18,19,13},{3,13,20},{12,16,3},{2,10,10},{3,11,4},{13,16,6}};
        solution.splitPainting(segments);
    }
    public List<List<Long>> splitPainting(int[][] segments) {
        List<List<Long>> result = new ArrayList<List<Long>> ();
        int max = 0;
        for (int[] segment: segments) {
            if (segment[1] > max)  {
                max = segment[1];
            }
        }
        long[][] change = new long[max + 1][2];
        for (int[] segment : segments) {
            change[segment[0]][0] += segment[2];
            change[segment[1]][1] -= segment[2];
        }
        long sum = 0, start = 0;
        for(int i = 0; i < change.length; ++i) {
            if(change[i][0] != 0 || change[i][1] != 0) {
                if(sum == 0) {
                    start = i;
                }
                if(sum != 0) {
                    result.add(Arrays.asList(start, (long)i, sum));
                    start = i;
                }
                sum += change[i][0] + change[i][1];
            }
        }
        return result;
    }
}
